"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 系统内部使用工具
Case Name   : gs_ctl start只使用-D指定正确数据库实例目录启动主机
Create At   : 2021/01/15
Owner       : opentestcase015
Description :
    1.关闭正在运行的主机
    2.指定正确数据库实例目录启动主机
    3.查看集群状态
    4.恢复集群状态
Expect      :
    1.关闭正在运行的主机成功
    2.指定正确数据库实例目录启动主机成功
    3.查看集群状态主机启动成功
    4.成功
History     :
    Modified by opentestcase027 2023/2/1: 优化用例步骤格式及一主两备判断方式
"""

import unittest
import os
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger

LOG = Logger()


class LogicalReplication(unittest.TestCase):
    def setUp(self):
        LOG.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.sh_primary = CommonSH('PrimaryDbUser')

    def test_system_internal_tools(self):
        LOG.info('--------step1:关闭正在运行的主机  expect:成功--------')
        msg = self.sh_primary.stop_db_instance()
        LOG.info(msg)
        self.assertIn(self.constant.GS_CTL_STOP_SUCCESS_MSG, msg)

        LOG.info('-------step2:指定正确数据库实例目录启动主机  expect:成功-------')
        msg = self.sh_primary.start_db_instance()
        LOG.info(msg)
        self.assertIn(self.constant.RESTART_SUCCESS_MSG, msg)

        LOG.info('--------step3:查看集群状态 expect:正常--------')
        status = self.sh_primary.get_db_cluster_status(param='status')
        LOG.info(status)
        self.assertTrue(status)

    def tearDown(self):
        LOG.info('--------step4:恢复集群状态 expect:成功--------')
        recover = self.sh_primary.restart_db_cluster()
        LOG.info(recover)
        self.assertTrue(recover)
        recover_msg = self.sh_primary.get_db_cluster_status(param='detail')
        LOG.info(recover_msg)
        self.assertTrue(self.constant.CLUSTER_NORMAL_MSG in recover_msg)
        LOG.info(f'--------{os.path.basename(__file__)} end--------')